/**
 * Moondance UML modeling tool
 * 
 * @author Dascalu Laurentiu (dascalu.laurentziu@gmail.com).
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package ro.pub.cs.se.moondance.xmi.converter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Base class for the available UML connections: Association, Direct
 * Association, Aggregation, Composition, Inheritance and Realization.
 */
public abstract class UMLConnectionXMI {
	/** Connection type */
	protected String type;
	
	/** Connection id */
	protected String xmiID;
	
	/** Connection name / label */
	protected String name;
	
	/** Source block id */
	protected String source;
	
	/** Destination block id */
	protected String destination;
	
	/** Label on the source end, if any */
	protected String sourceLabel;
	
	/** Label on the destination end, if any */
	protected String destinationLabel;

	/** Source description */
	protected String upperSourceType;
	protected String lowerSourceType;
	protected String upperSourceValue;
	protected String lowerSourceValue;
	
	/** Destination description */
	protected String upperDestinationType;
	protected String lowerDestinationType;
	protected String upperDestinationValue;
	protected String lowerDestinationValue;

	protected String visibility;
	
	public UMLConnectionXMI(String name, String type, String id, String source,
			String destination,	String sourceLabel, String destinationLabel,
			String upperSourceType, String lowerSourceType, String upperSourceValue,
			String lowerSourceValue, String upperDestinationType, String lowerDestinationType,
			String upperDestinationValue, String lowerDestinationValue) {
		this.name = name;
		this.type = type;
		this.xmiID = id;
		this.source = source;
		this.destination = destination;
		this.sourceLabel = sourceLabel;
		this.destinationLabel = destinationLabel;
		
		this.upperSourceType = upperSourceType;
		this.lowerSourceType = lowerSourceType;
		this.upperSourceValue = upperSourceValue;
		this.lowerSourceValue = lowerSourceValue;		
		
		this.upperDestinationType = upperDestinationType;
		this.lowerDestinationType = lowerDestinationType;
		this.upperDestinationValue = upperDestinationValue;
		this.lowerDestinationValue = lowerDestinationValue;
	}
	
	/**
	 * The format is based on reverse engineering on the Eclipse UML2.0 Tools plugin :-).
	 * Description of tag format of connections, in exported XMI file, is done
	 * in the upper classes.
	 */
	public void serialize(Element root, Document document) {
		// override
	}
	
	/**
	 * Serializes a connection
	 */
	protected void serialize(Element root, Document document, Element node,
			String generatedID, String associationID, String xmiID) {
		node.setAttribute(XMICommon.XMI_ID, generatedID);
		node.setAttribute(XMICommon.NAME, sourceLabel);
		node.setAttribute(XMICommon.TYPE, xmiID);
		node.setAttribute(XMICommon.ASSOCIATION, associationID);
	}
	
	/**
	 * Serializes upper/lower value
	 */
	protected void serialize(Element root, Document document, Element node,
			String generatedUpperID, String generatedLowerID, String upperType,
			String lowerType, String upperValue, String lowerValue) {
		Element upperElement = document.createElement(XMICommon.UPPER_VALUE);
		upperElement.setAttribute(XMICommon.XMI_ID, generatedUpperID);
		upperElement.setAttribute(XMICommon.XMI_TYPE, upperType);
		upperElement.setAttribute(XMICommon.VALUE, upperValue);
		
		Element lowerElement = document.createElement(XMICommon.LOWER_VALUE);
		lowerElement.setAttribute(XMICommon.XMI_ID, generatedLowerID);
		lowerElement.setAttribute(XMICommon.XMI_TYPE, lowerType);
		lowerElement.setAttribute(XMICommon.VALUE, lowerValue);
		
		node.appendChild(upperElement);
		node.appendChild(lowerElement);
	}
	
	public String getXMIid() {
		return xmiID;
	}
	
	public void setVisibility(String visibility) {
		this.visibility = visibility;
	}
}
